package org.jeecg.modules.cxf;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.jeecg.modules.integration.service.DataService;

/**
 * @Description
 * @Author linminfeng
 * @Date 2020-11-07 16:00
 */
public class CxfClient {
    public static String address = "http://127.0.0.1:8080/jeecg-boot/services/integration?wsdl";
    /**
     * 方式1.代理类工厂的方式,需要拿到对方的接口
     */
    public static void cl1() {
        try {
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress( address );
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass( DataService.class );
            // 创建一个代理接口实现
            DataService cs = (DataService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String userName = "Leftso";
            // 调用代理接口的方法调用并返回结果
            String result = cs.sayHello( userName );
            System.out.println( "返回结果:" + result );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description:动态调用方式
     * @Author: linminfeng
     * @Param: [serviceName 接口名, args 参数]
     * @Return: void
     * @Date: 2020-11-09 09:44
     */
    public static void invokeWebService(String serviceName,Object... args) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient( address );
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
        // PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke( serviceName, args );
            System.out.println( "返回数据:" + objects[0] );
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {

        invokeWebService("sayHello","Leftso");
    }
}
